package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.Contract;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.service.IContractService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 合同信息
 * </p>
 *
 * @author chenxuebin
 * @since 2018-11-26
 */
@RestController
public class ContractController {

    private static final Logger logger = LoggerFactory.getLogger(ContractController.class);

    @Autowired
    private IContractService contractService;

    @RequestMapping("/api/contract/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value = "status", required = false) String status,
                      @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                      @RequestParam(value = "pageSize", required = false) Integer pageSize,
                      HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Page page = new Page(pageIndex, pageSize);
        page = contractService.selectPage(page, new EntityWrapper<Contract>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/contract/add")
    @ResponseBody
    public Jinfo contractAdd(@Valid Contract contract,
                             BindingResult bindingResult, HttpServletRequest req) {
        Jinfo info = new Jinfo();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            info.setMsg("There are errors! " + bindingResult);
            info.setCode(400);
            return info;
        }
        contract.setCreatetime(new Date());
        contract.setMid(ObjectUtil.getMid());
        contract.setMpid(ObjectUtil.getMpid());
        contract.setUid(ObjectUtil.getUid());
        contract.insert();
        return info;
    }

    @RequestMapping("/api/contract/modify")
    @ResponseBody
    public Jinfo contractUpdate(HttpServletRequest req,
                                Contract contract) {
        Jinfo info = new Jinfo();
        contract.setModifytime(new Date());
        contract.updateById();
        return info;
    }

    @RequestMapping("/api/contract/detail")
    @ResponseBody
    public Jinfo getContractDetailById(@RequestParam("token") String token,
                                       @RequestParam("id") long id, HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        Contract contract = contractService.selectById(id);
        info.setInfo(contract);
        return info;
    }
}
